package view;

import model.MenuItem;
import model.MenuItemDAO;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.List;

public class CentralMenuView extends JFrame {
    private JTable menuTable;//显示菜单项
    private JButton updatePriceButton, addItemButton, deleteItemButton;//三个按钮更新，添加，删除
    private MenuItemDAO menuItemDAO;//与数据库进行交互

    public CentralMenuView() {
        setTitle("Menu Items Management");//标题
        setSize(800, 500);//窗口大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
        setLocationRelativeTo(null);//窗口居中

        menuItemDAO = new MenuItemDAO();//初始化数据库交互对象 创建一个对象并赋值

        // 菜单表格
        menuTable = new JTable();
        JScrollPane scrollPane = new JScrollPane(menuTable);
        loadMenuItems();

        // 更新价格按钮
        updatePriceButton = new JButton("更新价格");
        updatePriceButton.addActionListener(e -> updatePrice());

        // 添加新菜单项按钮
        addItemButton = new JButton("添加新菜单项");
        addItemButton.addActionListener(e -> addNewMenuItem());

        // 删除菜单项按钮
        deleteItemButton = new JButton("删除菜单项");
        deleteItemButton.addActionListener(e -> deleteMenuItem());

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(updatePriceButton);
        buttonPanel.add(addItemButton);
        buttonPanel.add(deleteItemButton);

        // 主布局
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(scrollPane, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        add(panel);
    }

    private void loadMenuItems() {//从数据库中加载菜单项
        try {
            // 获取所有菜单项
            List<MenuItem> items = menuItemDAO.getMenuItems();
            DefaultTableModel model = new DefaultTableModel(new Object[]{"ItemID", "StoreID", "Name", "Type", "Price"}, 0);
            for (MenuItem item : items) {
                model.addRow(new Object[]{item.getItemID(), item.getStoreID(), item.getItemName(), item.getItemType(), item.getPrice()});
            }//将菜单项添加到表格中 遍历菜单列表
            menuTable.setModel(model);//将DefaultTableModel设置给menuTable
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "加载菜单失败: " + e.getMessage());
        }//异常处理
    }

    private void updatePrice() {//更新价格
        try {
            int selectedRow = menuTable.getSelectedRow();
            if (selectedRow == -1) {
                JOptionPane.showMessageDialog(this, "请先选择一个菜单项！");
                return;
            }//获取用户选中的行索引

            int itemID = (int) menuTable.getValueAt(selectedRow, 0);
            String itemName = (String) menuTable.getValueAt(selectedRow, 2);
            double currentPrice = (double) menuTable.getValueAt(selectedRow, 4);//获取选中行各列的值

            String newPriceStr = JOptionPane.showInputDialog(this, "输入新的价格 (" + itemName + ", 当前价格: $" + currentPrice + "):");
            if (newPriceStr == null || newPriceStr.trim().isEmpty()) {
                return;
            }//弹框输入新价格

            double newPrice = Double.parseDouble(newPriceStr);//更新类型
            menuItemDAO.updatePrice(itemID, newPrice);//调用数据库更新价格
            loadMenuItems();//刷新
            JOptionPane.showMessageDialog(this, "价格更新成功！");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "更新价格失败: " + e.getMessage());
        }
    }

    private void addNewMenuItem() {//添加新的菜单项
        try {
            // 输入新菜单项信息
            String name = JOptionPane.showInputDialog(this, "输入新菜单项的名称:");
            if (name == null || name.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this, "名称不能为空！");
                return;
            }

            String type = JOptionPane.showInputDialog(this, "输入菜单项的类型 (如披萨、饮料、甜点):");
            if (type == null || type.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this, "类型不能为空！");
                return;
            }

            String priceStr = JOptionPane.showInputDialog(this, "输入菜单项的价格:");
            if (priceStr == null || priceStr.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this, "价格不能为空！");
                return;
            }

            double price = Double.parseDouble(priceStr);//转换类型

            String storeIDStr = JOptionPane.showInputDialog(this, "输入门店ID:");
            if (storeIDStr == null || storeIDStr.trim().isEmpty()) {
                JOptionPane.showMessageDialog(this, "门店ID不能为空！");
                return;
            }

            int storeID = Integer.parseInt(storeIDStr);//转换类型

            // 添加菜单项到数据库
            MenuItem newItem = new MenuItem(0, storeID, name, type, price);
            menuItemDAO.addMenuItem(newItem);//调用addMenuItem方法添加新菜单项
            loadMenuItems();//刷新
            JOptionPane.showMessageDialog(this, "成功添加新菜单项: " + name);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "价格或门店ID必须是数字！");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "添加菜单项失败: " + e.getMessage());//异常
        }
    }

    private void deleteMenuItem() {//删除选定的菜单项
        try {
            int selectedRow = menuTable.getSelectedRow();//获取用户选中的行索引
            if (selectedRow == -1) {
                JOptionPane.showMessageDialog(this, "请先选择一个菜单项！");
                return;
            }//判断

            int itemID = (int) menuTable.getValueAt(selectedRow, 0);//获取选中行的id
            menuItemDAO.deleteMenuItem(itemID);//调用deleteMenuItem方法删除菜单项
            loadMenuItems();//重载
            JOptionPane.showMessageDialog(this, "菜单项已成功删除！");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "删除菜单项失败: " + e.getMessage());
        }//异常
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new CentralMenuView().setVisible(true));
    }
}
